package collectiondemo;

//以下实例演示了如何使用 Collection 类的 Collections.unmodifiableList() 方法来设置集合为只读：
public class Main8 {
    public static void main(String[] args) {
        java.util.List stuff = java.util.Arrays.asList("a", "b");
        java.util.List list = new java.util.ArrayList(stuff);
        list = java.util.Collections.unmodifiableList(list);
        try {
            list.set(0, "new value");
        } catch (UnsupportedOperationException e) {
            System.out.println("集合现在是只读");
//            e.printStackTrace();
            throw new RuntimeException(e);
        }
        java.util.Set set = new java.util.HashSet(stuff);
        set = java.util.Collections.unmodifiableSet(set);
        try {
            java.util.Map map = new java.util.HashMap();
            map = java.util.Collections.unmodifiableMap(map);
            System.out.println("集合现在是只读");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
